﻿@model AntUnion.FrameWork.Web.Areas.Console.Controllers.WeChatTemplateModel

@{
    ViewData["Title"] = "Create";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <style>
        .testPicdiv {
            width: 200px;
            height: 100px;
            overflow: auto;
            border: none;
        }

        .layui-input-disabled {
            background-color: #f2f2f2;
        }

        .scrollbar::-webkit-scrollbar { /*滚动条整体样式*/
            width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
            height: 1px;
        }

        .scrollbar::-webkit-scrollbar-thumb { /*滚动条里面小方块*/
            border-radius: 10px;
            -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            background: #009688;
        }

        .scrollbar::-webkit-scrollbar-track { /*滚动条里面轨道*/
            -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            border-radius: 10px;
            background: #EDEDED;
        }

        .layui-form-label {
            width: 10% !important;
        }

        .layui-input {
            width: 100%;
        }

        .layui-input-block {
            margin-left: 180px !important;
        }

        .layui-input-inline {
            width: 15% !important;
        }

        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }

        input[type="number"] {
            -moz-appearance: textfield;
        }
    </style>
    <script src="~/js/CommonHelper.js"></script>
    <script type="text/javascript">
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form', 'upload', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , upload = layui.upload
                , element = layui.element
                , laydate = layui.laydate
                , parentFrameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引;
            element.init();

            /* 监听提交 */
            form.on('submit(updateWechatTemplate)', function (data) {
                var datas = data.field;
                var i = 1;
                var templatedatalist = [];

                //获取模板字段数量
                var templatedatacount = $('#keyword').children('.layui-form-item').length

                for (var i = 1; i <= templatedatacount; i++) {
                    var templatedata = new Object();
                    var order_numbermin = '#order_numbermin' + i;
                    var order_numbervalue = '#order_numbervalue' + i;
                    if ($(order_numbermin).val() != "" && $(order_numbermin).val() != null && $(order_numbervalue).val() != "" && $(order_numbervalue).val() != null) {
                        templatedata.id = Number($(order_numbermin).val());
                        templatedata.explain = $(order_numbervalue).val();
                        templatedatalist.push(templatedata);
                    }
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/SystemConfig/DeliveryFee",
                    data: { id: $("#id").val(), model: JSON.stringify(templatedatalist) }, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            layer.msg("编辑成功", { icon: 1, time: 1000 }, function () {
                            });
                        } else if (item.code == 1) {
                            layer.msg(item.data, { icon: 2 });
                        } else {
                            layer.msg("保存失败", { icon: 0 });
                        }
                        layer.closeAll('loading');
                    }
                });
                return false;
            });

            let i = 0;

            $('#addkeyword').click(function () {
                i++;
                addHtmlObj(i,"","");
                $('#removekeyword' + i).click(function () {
                    i--;
                    $(this).parent().remove()
                });
            });

            function addHtmlObj(i, id, explain) {
                let addhtml;
                addhtml = '<div class="layui-form-item">'
                    + '<div>'
                    + '<label class="layui-form-label">编号' + i + '</label>'
                    + '<div class="layui-input-inline">'
                    + '<input type="number" id="order_numbermin' + i + '" name="order_numbermin' + i + '" value="' + id +'" maxlength="50" lay-verify="order_numbermin" placeholder="" autocomplete="off" class="layui-input">'
                    + '</div>'
                    + '<div>'
                    + '<label class="layui-form-label">说明' + i + '</label>'
                    + '<div class="layui-input-inline">'
                    + '<input type="text" id="order_numbervalue' + i + '" name="order_numbervalue' + i + '" value="' + explain +'" maxlength="50" placeholder="" autocomplete="off" class="layui-input">'
                    + '</div>'
                    + '</div>'
                    + '<button type="button" class="layui-btn layui-btn-primary" id="removekeyword' + i + '">移除</button>'
                    + '</div>'
                $('#keyword').append(addhtml);

            }

            $(function () {
                var list = '@Html.Raw(ViewBag.dfd)';
                if (list != "" && list != null) {
                    var data = JSON.parse(list);
                    for (var a = 0; a < data.length; a++) {
                        i++;
                        addHtmlObj(i, data[a].id, data[a].explain);
                        $('#removekeyword' + i).click(function () {
                             i--;
                             console.log(i);
                            $(this).parent().remove()
                         });
                    }

                }
            });
        });
    </script>
}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px; ">
            <form class="layui-form layui-form-pane" action="" lay-filter="component-form-group">
                <input type="text" id="id" value="@ViewBag.id" style="display:none;" />
                <div id="keyword">
                </div>
                <button type="button" class="layui-btn" id="addkeyword">添加 </button>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="updateWechatTemplate">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>